from ..ScriptWidget import ScriptWidget
from enums.WidgetType import WidgetType
from PySide6.QtWidgets import QLabel, QLineEdit


class VariableSet(ScriptWidget):
    def __init__(self, initType=None):
        super(VariableSet, self).__init__(WidgetType.VARIABLE_SET)

        self.initType = initType

        labTitle = QLabel('变量:', self)
        labTitle.setFixedSize(30, self.lineHeight)
        self.addWidget(labTitle)

        self.newLineSpaceWidth = labTitle.width() + self.intervalX

        self.editVarName = QLineEdit(self)
        self.editVarName.setMaxLength(10)
        self.editVarName.setFixedSize(100, self.lineHeight)
        self.addWidget(self.editVarName)

        labEqual = QLabel('=', self)
        labEqual.setFixedSize(10, self.lineHeight)
        self.addWidget(labEqual)

        self.editVarValue = QLineEdit(self)
        self.editVarValue.setMaxLength(10)
        self.editVarValue.setFixedSize(100, self.lineHeight)
        self.addWidget(self.editVarValue)

        if initType == 'create' or initType == 'edit':
            self.addOperate()

    @property
    def data(self):
        return {
            "type": self.type.value,
            "name": self.editVarName.text(),
            "value": self.editVarValue.text(),
            'enable': self.switchEnable.isChecked()
        }

    @data.setter
    def data(self, data):
        self.setData(data)
        self.editVarName.setText(data['name'])
        self.editVarValue.setText(data['value'])
